﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using System.Configuration;

namespace CommonLib
{
    public class ParamElement : ConfigurationElement
    {
        private static readonly ConfigurationProperty paramKey = new ConfigurationProperty("Key", typeof(string), string.Empty, ConfigurationPropertyOptions.IsRequired);
        private static readonly ConfigurationProperty paramValue = new ConfigurationProperty("Value", typeof(string), string.Empty, ConfigurationPropertyOptions.IsRequired);

        public ParamElement()
        {
            base.Properties.Add(paramKey);
            base.Properties.Add(paramValue);
        }

        [ConfigurationProperty("Key", IsRequired = true)]
        public string Key
        {
            get { return (string)this[paramKey]; }
        }

        [ConfigurationProperty("Value", IsRequired = true)]
        public string Value
        {
            get { return (string)this[paramValue]; }
        }
    }

    [ConfigurationCollection(typeof(ParamElement), AddItemName = "Param", CollectionType = ConfigurationElementCollectionType.BasicMap)]
    public class ParamCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new ParamElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ParamElement)element).Key;
        }

        new public ParamElement this[string name]
        {
            get { return (ParamElement)BaseGet(name); }
        }
    }



    public class DomainConfigurationElement : ConfigurationElement
    {
        private static  ConfigurationProperty nameAttribute = new ConfigurationProperty("Name", typeof(string), string.Empty, ConfigurationPropertyOptions.IsRequired);
        private static  ConfigurationProperty patternAttribute = new ConfigurationProperty("Pattern", typeof(string), string.Empty, ConfigurationPropertyOptions.IsRequired);
        private static  ConfigurationProperty paramsElement = new ConfigurationProperty("Params", typeof(ParamCollection), null, ConfigurationPropertyOptions.IsRequired);

        public DomainConfigurationElement()
        {
            base.Properties.Add(nameAttribute);
            base.Properties.Add(patternAttribute);
            base.Properties.Add(paramsElement);
        }

        [ConfigurationProperty("Name", IsRequired = true)]
        public string Name
        {
            get { return (string)this[nameAttribute]; }
        }

        [ConfigurationProperty("Pattern", IsRequired = true)]
        public string Pattern
        {
            get { return (string)this[patternAttribute]; }
        }

        [ConfigurationProperty("Params", IsRequired = true)]
        public ParamCollection Params
        {
            get { return (ParamCollection)this[paramsElement]; }
        }
    }

    [ConfigurationCollection(typeof(DomainConfigurationElement), AddItemName = "DomainConfiguration", CollectionType = ConfigurationElementCollectionType.BasicMap)]
    public class DomainsConfig : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new DomainConfigurationElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((DomainConfigurationElement)element).Name;
        }

        new public DomainConfigurationElement this[string name]
        {
            get { return (DomainConfigurationElement)BaseGet(name); }
        }
    }



    public class DomainExtractionConfig : ConfigurationSection
    {
        private static readonly ConfigurationProperty domainsConfig = new ConfigurationProperty("DomainsConfig", typeof(DomainsConfig), null, ConfigurationPropertyOptions.IsRequired);

        public DomainExtractionConfig()
        {
            base.Properties.Add(domainsConfig);
        }

        [ConfigurationProperty("DomainsConfig", IsRequired = true)]
        public DomainsConfig DomainsConfig
        {
            get { return (DomainsConfig)this[domainsConfig]; }
        }
    }

    //public class ConfigurationManager
    //{
    //    public static string configurationDBName = "configuration_sqlite.db";
    //    public static bool SetConfigurationForUrl(string domain, string pattern)
    //    {
    //        try
    //        {
    //            SQLiteHelper.connectionString = "Data Source=" + configurationDBName;
    //            SQLiteConnection conn = new SQLiteConnection();
    //            SQLiteConnectionStringBuilder connstr = new SQLiteConnectionStringBuilder();
    //            connstr.DataSource = configurationDBName;
    //            conn.ConnectionString = connstr.ToString();
    //            conn.Open();

    //            SQLiteCommand cmd = new SQLiteCommand();
    //            cmd.CommandText = "create table if not exists DomainConfig (domain PRIMARY KEY, urlPattern);";
    //            cmd.Connection = conn;
    //            cmd.ExecuteNonQuery();

    //            string insertSql = "insert into DomainConfig values ('{0}', '{1}');";
    //            SQLiteCommand insertCommand = new SQLiteCommand();
    //            insertCommand.Connection = conn;
    //            insertCommand.CommandText = string.Format(insertSql, domain, pattern);
    //            insertCommand.ExecuteNonQuery();
    //            return true;
    //        }
    //        catch (Exception e)
    //        {
    //            Console.WriteLine(e.Message);
    //            return false;
    //        }

    //    }


    //}
}
